﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using kad2SimSoft.Classes;
using kad2SimSoft.Resources.Lokalisierung;

namespace kad2SimSoft
{
    /// <summary>
    /// Interaktionslogik für KeyData.xaml
    /// </summary>
    public partial class KeyDataControl : UserControl
    {
        private KeyData _keydata;
        public KeyDataControl()
        {
            InitializeComponent();
        }

        private void FillValuesOnControl()
        {
            int currentPeriod = FindPageControl(this).ActPeriod - 1;
            String periodAppendix = " (" + kad2SimSoft.Properties.Resources.lbPeriod + " " + currentPeriod + ")";
            String prefix;
            double currentValue;
            double averageValue;

            lbOverallHeader.Content = kad2SimSoft.Properties.Resources.lbOverallData + periodAppendix;
            lbSalesHeader.Content = kad2SimSoft.Properties.Resources.lbSalesData + periodAppendix;
            lbProductionHeader.Content = kad2SimSoft.Properties.Resources.lbProductionData + periodAppendix;
            lbStorageHeader.Content = kad2SimSoft.Properties.Resources.lbWarehouseData + periodAppendix;

            # region ColumnLabels
            lbOverallCurrent.Content = kad2SimSoft.Properties.Resources.lbCurrent + " (" + currentPeriod + ")";
            lbSalesCurrent.Content = kad2SimSoft.Properties.Resources.lbCurrent + " (" + currentPeriod + ")";
            lbProductionCurrent.Content = kad2SimSoft.Properties.Resources.lbCurrent + " (" + currentPeriod + ")";
            lbStorageCurrent.Content = kad2SimSoft.Properties.Resources.lbCurrent + " (" + currentPeriod + ")";
            if (FindPageControl(this).ActPeriod > 1)
            {
                lbOverallAverage.Content = kad2SimSoft.Properties.Resources.lbAverage + " (1-" + currentPeriod + ")";
                lbOverallAllPeriods.Content = kad2SimSoft.Properties.Resources.lbAllPeriods + " (1-" + currentPeriod + ")";

                lbSalesAverage.Content = kad2SimSoft.Properties.Resources.lbAverage + " (1-" + currentPeriod + ")";
                lbSalesAllPeriods.Content = kad2SimSoft.Properties.Resources.lbAllPeriods + " (1-" + currentPeriod + ")";

                lbProductionAverage.Content = kad2SimSoft.Properties.Resources.lbAverage + " (1-" + currentPeriod + ")";
                lbProductionAll.Content = kad2SimSoft.Properties.Resources.lbAllPeriods + " (1-" + currentPeriod + ")";

                lbStorageAverage.Content = kad2SimSoft.Properties.Resources.lbAverage + " (1-" + currentPeriod + ")";
                lbStorageAll.Content = kad2SimSoft.Properties.Resources.lbAllPeriods + " (1-" + currentPeriod + ")";
            }
            else
            {
                lbOverallAverage.Content = kad2SimSoft.Properties.Resources.lbAverage;
                lbOverallAllPeriods.Content = kad2SimSoft.Properties.Resources.lbAllPeriods;

                lbSalesAverage.Content = kad2SimSoft.Properties.Resources.lbAverage + " (1-" + currentPeriod + ")";
                lbSalesAllPeriods.Content = kad2SimSoft.Properties.Resources.lbAllPeriods + " (1-" + currentPeriod + ")";

                lbProductionAverage.Content = kad2SimSoft.Properties.Resources.lbAverage + " (1-" + currentPeriod + ")";
                lbProductionAll.Content = kad2SimSoft.Properties.Resources.lbAllPeriods + " (1-" + currentPeriod + ")";

                lbStorageAverage.Content = kad2SimSoft.Properties.Resources.lbAverage + " (1-" + currentPeriod + ")";
                lbStorageAll.Content = kad2SimSoft.Properties.Resources.lbAllPeriods + " (1-" + currentPeriod + ")";
            }
            # endregion

            # region OverallData
            # region Profit
            currentValue = Convert.ToDouble(_keydata.Profit.CurrentValue);
            averageValue = Convert.ToDouble(_keydata.Profit.Average);
            prefix = "";

            var uri = new Uri(returnTrendArrowString(currentValue, averageValue, true));
            var bitmap = new BitmapImage(uri);
            imgProfCurr.Source = bitmap;
            imgProfCurr.Height = resizeImageIfNecessary(uri.AbsolutePath);

            if (currentValue != averageValue)
            {
                if (isValueSmallerThan(currentValue, averageValue)) { prefix = "-"; } else { prefix = "+"; };
            }

            tbProfCurrent.Text = _keydata.Profit.CurrentValue.ToString() + "€ (" + prefix + String.Format("{0:0.00}%", returnPercentageDifference(currentValue, averageValue)) + ")*";
            tbProfAverage.Text = _keydata.Profit.Average.ToString() + "€";
            if (!_keydata.Profit.ToString().Equals("-")) 
            { 
                tbProfAll.Text = _keydata.Profit.All.ToString() + "€"; 
            } else 
            { 
                tbProfAll.Text = _keydata.Profit.All.ToString();
            }
            # endregion

            # region ProfitperUnit
            currentValue = Convert.ToDouble(_keydata.Profitperunit.CurrentValue);
            averageValue = Convert.ToDouble(_keydata.Profitperunit.Average);
            prefix = "";

            uri = new Uri(returnTrendArrowString(currentValue, averageValue, true));
            bitmap = new BitmapImage(uri);
            imgProfUnitCurr.Source = bitmap;

            if (currentValue != averageValue)
            {
                if (isValueSmallerThan(currentValue, averageValue)) { prefix = "-"; } else { prefix = "+"; };
            }

            tbProfUnitCurrent.Text = _keydata.Profitperunit.CurrentValue.ToString() + "€ (" + prefix + String.Format("{0:0.00}%", returnPercentageDifference(currentValue, averageValue)) + ")*";
            imgProfUnitCurr.Height = resizeImageIfNecessary(uri.AbsolutePath);
            tbProfUnitAverage.Text = _keydata.Profitperunit.Average.ToString() + "€";
            tbProfUnitAll.Text = _keydata.Profitperunit.All.ToString();
            # endregion
            # endregion

            # region SalesData
            # region Sellwish
            currentValue = Convert.ToDouble(_keydata.Sellwish.CurrentValue);
            averageValue = Convert.ToDouble(_keydata.Sellwish.Average);
            prefix = "";
            if (currentValue != averageValue)
            {
                if (isValueSmallerThan(currentValue, averageValue)) { prefix = "-"; } else { prefix = "+"; };
            }
            uri = new Uri(returnTrendArrowString(currentValue, averageValue, true));
            bitmap = new BitmapImage(uri);
            imgSellwish.Source = bitmap;
            imgSellwish.Height = resizeImageIfNecessary(uri.AbsolutePath);

            tbSellwishCurrent.Text = _keydata.Sellwish.CurrentValue.ToString() + " (" + prefix + String.Format("{0:0.00}%", returnPercentageDifference(currentValue, averageValue))+ ")*";
            tbSellwishAverage.Text = _keydata.Sellwish.Average.ToString();
            tbSellwishAllPeriods.Text = _keydata.Sellwish.All.ToString();
            # endregion

            # region SalesQuantity
            currentValue = Convert.ToDouble(_keydata.Salesquantity.CurrentValue);
            averageValue = Convert.ToDouble(_keydata.Salesquantity.Average);
            prefix = "";

            if (currentValue != averageValue)
            {
                if (isValueSmallerThan(currentValue, averageValue)) { prefix = "-"; } else { prefix = "+"; };
            }
            uri = new Uri(returnTrendArrowString(currentValue, averageValue, true));
            bitmap = new BitmapImage(uri);
            imgSold.Source = bitmap;
            imgSold.Height = resizeImageIfNecessary(uri.AbsolutePath);
            tbSoldCurr.Text = _keydata.Salesquantity.CurrentValue.ToString() + " (" + prefix + String.Format("{0:0.00}%", returnPercentageDifference(currentValue, averageValue))+ ")*";
            tbSoldAverage.Text = _keydata.Salesquantity.Average.ToString();
            tbSoldAll.Text = _keydata.Salesquantity.All.ToString();
            # endregion

            #region Reliability
            currentValue = Convert.ToDouble(_keydata.Deliveryreliability.CurrentValue.Replace("%",""));
            averageValue = Convert.ToDouble(_keydata.Deliveryreliability.CurrentValue.Replace("%", ""));
            prefix = "";

            if (currentValue != averageValue)
            {
                if (isValueSmallerThan(currentValue, averageValue)) { prefix = "-"; } else { prefix = "+"; };
            }

            uri = new Uri(returnTrendArrowString(currentValue, averageValue, true));
            bitmap = new BitmapImage(uri);
            imgReliability.Source = bitmap;
            imgReliability.Height = resizeImageIfNecessary(uri.AbsolutePath);

            tbRelCurr.Text = _keydata.Deliveryreliability.CurrentValue.ToString() + " (" + prefix + String.Format("{0:0.00}%", returnPercentageDifference(currentValue, averageValue)) + ")*";
            tbRelAvg.Text = _keydata.Deliveryreliability.Average.ToString();
            if (!_keydata.Deliveryreliability.All.ToString().Equals("-"))
            {
                tbRelAll.Text = _keydata.Deliveryreliability.All.ToString() + " %";
            }
            else
            {
                tbRelAll.Text = _keydata.Deliveryreliability.All.ToString();
            }
            #endregion

            #region Marketplacesale
            currentValue = Convert.ToDouble(_keydata.Marketplacesale.CurrentValue);
            averageValue = Convert.ToDouble(_keydata.Marketplacesale.Average);
            prefix = "";

            if (currentValue != averageValue)
            {
                if (isValueSmallerThan(currentValue, averageValue)) { prefix = "-"; } else { prefix = "+"; };
            }
            uri = new Uri(returnTrendArrowString(currentValue, averageValue, true));
            bitmap = new BitmapImage(uri);
            imgMarketsales.Source = bitmap;

            tbMarketCurr.Text = _keydata.Marketplacesale.CurrentValue.ToString() + "€ (" + prefix + String.Format("{0:0.00}%", returnPercentageDifference(currentValue, averageValue)) + ")*";
            tbMarketAvg.Text = _keydata.Marketplacesale.Average.ToString() + "€";
            tbMarketAll.Text = _keydata.Marketplacesale.All.ToString() + "€";
            # endregion
            # endregion

            # region ProductionData

            # region Capacity
            tbCapCurr.Text = _keydata.Capacity.CurrentValue.ToString() + " min";
            tbCapAvg.Text = _keydata.Capacity.Average.ToString() + " min";
            tbCapAll.Text = _keydata.Capacity.All.ToString() + " min";

            tbPosCapCurr.Text = _keydata.Possiblecapacity.CurrentValue.ToString() + " min";
            tbPosCapAvg.Text = _keydata.Possiblecapacity.Average.ToString() + " min";
            tbPosCapAll.Text = _keydata.Possiblecapacity.All.ToString() + " min";
            #endregion

            #region Posible Capacity
            currentValue = Convert.ToDouble(_keydata.Possiblecapacity.CurrentValue);
            averageValue = Convert.ToDouble(_keydata.Possiblecapacity.Average);
            prefix = "";

            if (currentValue != averageValue)
            {
                if (isValueSmallerThan(currentValue, averageValue)) { prefix = "-"; } else { prefix = "+"; };
            }

            tbPosCapCurr.Text = _keydata.Possiblecapacity.CurrentValue.ToString() + "min (" + prefix + String.Format("{0:0.00}%", returnPercentageDifference(currentValue, averageValue)) + ")*";
            tbPosCapAvg.Text = _keydata.Possiblecapacity.Average.ToString() + "min";
            tbPosCapAll.Text = _keydata.Possiblecapacity.All.ToString() + "min";
            #endregion

            # region RelpossibleNormalCapacity
            currentValue = Convert.ToDouble(_keydata.Relpossiblenormalcapacity.CurrentValue.Replace("%",""));
            averageValue = Convert.ToDouble(_keydata.Relpossiblenormalcapacity.Average.Replace("%",""));
            prefix = "";

            if (currentValue != averageValue)
            {
                if (isValueSmallerThan(currentValue, averageValue)) { prefix = "-"; } else { prefix = "+"; };
            }

            tbRelCapCurr.Text = _keydata.Relpossiblenormalcapacity.CurrentValue.ToString() + " (" + prefix + String.Format("{0:0.00}%", returnPercentageDifference(currentValue, averageValue)) + ")*";
            tbRelCapAvg.Text = _keydata.Relpossiblenormalcapacity.Average.ToString();
            tbRelCapAll.Text = "-";
            # endregion

            # region Productive Time
            currentValue = Convert.ToDouble(_keydata.Productivetime.CurrentValue);
            averageValue = Convert.ToDouble(_keydata.Productivetime.Average);
            prefix = "";

            if (currentValue != averageValue)
            {
                if (isValueSmallerThan(currentValue, averageValue)) { prefix = "-"; } else { prefix = "+"; };
            }
            uri = new Uri(returnTrendArrowString(currentValue, averageValue, true));
            bitmap = new BitmapImage(uri);
            imgProdTime.Source = bitmap;
            imgProdTime.Height = resizeImageIfNecessary(uri.AbsolutePath);

            tbProdTCurr.Text = _keydata.Productivetime.CurrentValue.ToString() + "min (" + prefix + String.Format("{0:0.00}%", returnPercentageDifference(currentValue, averageValue)) + ")*";
            tbProdTAvg.Text = _keydata.Productivetime.Average.ToString() + "min";
            tbProdTAll.Text = _keydata.Productivetime.All.ToString() + "min";
            #endregion

            # region Effiency
            currentValue = Convert.ToDouble(_keydata.Effiency.CurrentValue.Replace("%",""));
            averageValue = Convert.ToDouble(_keydata.Effiency.Average.Replace("%",""));
            prefix = "";

            if (currentValue != averageValue)
            {
                if (isValueSmallerThan(currentValue, averageValue)) { prefix = "-"; } else { prefix = "+"; };
            }

            uri = new Uri(returnTrendArrowString(currentValue, averageValue, true));
            bitmap = new BitmapImage(uri);
            imgEffiency.Source = bitmap;
            imgEffiency.Height = resizeImageIfNecessary(uri.AbsolutePath);

            tbEffCurr.Text = _keydata.Effiency.CurrentValue.ToString() + " (" + prefix + String.Format("{0:0.00}%", returnPercentageDifference(currentValue, averageValue)) + ")*";
            tbEffAvg.Text = _keydata.Effiency.Average.ToString();
            tbEffAll.Text = "-";
            #endregion

            #region Idletime
            currentValue = Convert.ToDouble(_keydata.Idletime.CurrentValue);
            averageValue = Convert.ToDouble(_keydata.Idletime.Average);
            prefix = "";

            if (currentValue != averageValue)
            {
                if (isValueSmallerThan(currentValue, averageValue)) { prefix = "-"; } else { prefix = "+"; };
            }

            uri = new Uri(returnTrendArrowString(currentValue, averageValue, false));
            bitmap = new BitmapImage(uri);
            imgIdleTime.Source = bitmap;
            imgIdleTime.Height = resizeImageIfNecessary(uri.AbsolutePath);

            tbIdleTCurr.Text = _keydata.Idletime.CurrentValue.ToString() + "min (" + prefix + String.Format("{0:0.00}%", returnPercentageDifference(currentValue, averageValue)) + ")*";
            tbIdleTAvg.Text = _keydata.Idletime.Average.ToString() + "min";
            tbIdleTAll.Text = _keydata.Idletime.All.ToString() + "min";
            #endregion

            #region Idletimecosts
            currentValue = Convert.ToDouble(_keydata.Idletimecosts.CurrentValue);
            averageValue = Convert.ToDouble(_keydata.Idletimecosts.Average);
            prefix = "";

            if (currentValue != averageValue)
            {
                if (isValueSmallerThan(currentValue, averageValue)) { prefix = "-"; } else { prefix = "+"; };
            }

            uri = new Uri(returnTrendArrowString(currentValue, averageValue, false));
            bitmap = new BitmapImage(uri);
            imgIdleTimeCosts.Source = bitmap;
            imgIdleTimeCosts.Height = resizeImageIfNecessary(uri.AbsolutePath);
            tbIdleCCurr.Text = _keydata.Idletimecosts.CurrentValue.ToString() + "€ (" + prefix + String.Format("{0:0.00}%", returnPercentageDifference(currentValue, averageValue)) + ")*";
            tbIdleCAvg.Text = _keydata.Idletimecosts.Average.ToString() + "€";
            tbIdleCAll.Text = _keydata.Idletimecosts.All.ToString() + "€";
            #endregion

            # endregion

            # region StorageData

            #region Storevalue
            currentValue = Convert.ToDouble(_keydata.Storevalue.CurrentValue);
            averageValue = Convert.ToDouble(_keydata.Storevalue.Average);
            prefix = "";

            if (currentValue != averageValue)
            {
                if (isValueSmallerThan(currentValue, averageValue)) { prefix = "-"; } else { prefix = "+"; };
            }

            tbStoreVCurr.Text = _keydata.Storevalue.CurrentValue.ToString() + "€ (" + prefix + String.Format("{0:0.00}%", returnPercentageDifference(currentValue, averageValue)) + ")*";
            tbStoreVAvg.Text = _keydata.Storevalue.Average.ToString() + "€";
            tbStoreVAll.Text = _keydata.Storevalue.All.ToString() + "€";
            #endregion

            #region Storagecosts
            currentValue = Convert.ToDouble(_keydata.Storagecosts.CurrentValue);
            averageValue = Convert.ToDouble(_keydata.Storagecosts.Average);
            prefix = "";

            if (currentValue != averageValue)
            {
                if (isValueSmallerThan(currentValue, averageValue)) { prefix = "-"; } else { prefix = "+"; };
            }

            uri = new Uri(returnTrendArrowString(currentValue, averageValue, false));
            bitmap = new BitmapImage(uri);
            imgStorageCosts.Source = bitmap;
            imgStorageCosts.Height = resizeImageIfNecessary(uri.AbsolutePath);
            tbStoreCCurr.Text = _keydata.Storagecosts.CurrentValue.ToString() + "€ (" + prefix + String.Format("{0:0.00}%", returnPercentageDifference(currentValue, averageValue)) + ")*";
            tbStoreCAvg.Text = _keydata.Storagecosts.Average.ToString() + "€";
            tbStoreCAll.Text = _keydata.Storagecosts.All.ToString() + "€";
            #endregion

            # endregion
        }

        public static MainWindow FindPageControl(DependencyObject child)
        {
            DependencyObject parent = VisualTreeHelper.GetParent(child);

            if (parent == null) return null;

            MainWindow page = parent as MainWindow;
            if (page != null)
            {
                return page;
            }
            else
            {
                return FindPageControl(parent);
            }
        }

        private void KeyDataControl_Loaded(object sender, RoutedEventArgs e)
        {
            _keydata = FindPageControl(this).Keydata;
            FillValuesOnControl();
        }

        private String returnTrendArrowString(double currentValue, double averageValue, bool greenArrowUp)
        {
            double percentageDifference = returnPercentageDifference(currentValue, averageValue);
            bool isCurrentValueSmaller = isValueSmallerThan(currentValue, averageValue);

            if (percentageDifference >= 0 && percentageDifference < 5)
            {
                return "pack://application:,,,/Resources/Images/TrendArrows/mid.png";
            }
            else if (percentageDifference >= 5 && percentageDifference < 25)
            {
                if (isCurrentValueSmaller)
                {
                    if (greenArrowUp)
                    {
                        return "pack://application:,,,/Resources/Images/TrendArrows/red_mid_arrow.png";
                    }
                    else
                    {
                        return "pack://application:,,,/Resources/Images/TrendArrows/green_down_mid.png";
                    }
                }
                else
                {
                    if (greenArrowUp)
                    {
                        return "pack://application:,,,/Resources/Images/TrendArrows/up_mid_arrow.png";
                    }
                    else
                    {
                        return "pack://application:,,,/Resources/Images/TrendArrows/red_up_mid.png";
                    }
                }
            }
            else
            {
                if (isCurrentValueSmaller)
                {
                    if (greenArrowUp)
                    {
                        return "pack://application:,,,/Resources/Images/TrendArrows/red_down_arrow.png";
                    }
                    else
                    {
                        return "pack://application:,,,/Resources/Images/TrendArrows/green_down.png";
                    }
                }
                else
                {
                    if (greenArrowUp)
                    {
                        return "pack://application:,,,/Resources/Images/TrendArrows/up_arrow.png";
                    }
                    else
                    {
                        return "pack://application:,,,/Resources/Images/TrendArrows/red_up.png";
                    }
                }
            }
        }

        private int resizeImageIfNecessary(String imageName)
        {
            if ((imageName.Contains("up") || imageName.Contains("down")) && (!imageName.Contains("mid")))
            {
                return 20;
            }
            return 15;
        }

        private double returnPercentageDifference(double value1, double value2)
        {
            if (value1 == 0 && value2 == 0.0)
            {
                value1 = 1;
                value2 = 1;
            }
            if (value1 <= value2)
            {
                return 100 - ((value1 / value2) * 100);
            }
            else
            {
                return ((value1/value2)*100) - 100;
            }
        }

        private bool isValueSmallerThan(double value1, double value2)
        {
            if (value1 <= value2)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
